/*
 *  Copyright 2008 Danny Lagrouw
 *  
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *  
 *      http://www.apache.org/licenses/LICENSE-2.0
 *  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License. 
 */
package org.bastion.domain;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.bastion.adapter.Adapter;
import org.bastion.message.DomainMessage;
import org.bastion.message.FindMessage;
import org.bastion.message.RegisterMessage;

public class Domain {

    private final static ThreadLocal<Domain> instance = new ThreadLocal<Domain>(); 
    
	@SuppressWarnings("unchecked")
	private HashMap<Class, List<Adapter>> adapters = new HashMap<Class, List<Adapter>>();
    
    private Domain() {
    }
    
    public static boolean isInitialized() {
    	return instance.get() != null;
    }
    
    public static Domain getInstance() {
        Domain domain = instance.get();
        if (domain == null) {
        	domain = new Domain();
        	instance.set(domain);
        }
        return domain;
    }

    /**
     * Clears all adapter registrations from the Domain instance. Only useful
     * for testing different domain setups.
     */
    public void clearAdapters() {
        adapters.clear();
    }
    
    @SuppressWarnings("unchecked")
	public <T extends DomainMessage> void registerAdapter(Class<T> messageClass, Adapter<T> adapter) {
    	List<Adapter> messageAdapters = adapters.get(messageClass);
    	if (messageAdapters == null) {
    		messageAdapters = new ArrayList<Adapter>();
    		adapters.put(messageClass, messageAdapters);
    	}
    	messageAdapters.add(adapter);
    }

    /**
     * Registers the object with the domain. Listening services
     * may assign a unique ID number to the object.
     * @param object object to be registered.
     * @return a unique ID number for the object, or null.
     */
    public static Long register(Object object) {
    	RegisterMessage message = notify(new RegisterMessage(object));
    	return message.getId();
    }
    
    public static <T extends DomainMessage> T notify(T message) {
    	return Domain.getInstance().notifyInternal(message);
    }
    
    @SuppressWarnings("unchecked")
	protected <T extends DomainMessage> T notifyInternal(T message) {
        List<Adapter> messageAdapters = adapters.get(message.getClass());
        if (messageAdapters != null) {
        	for (Adapter<? super DomainMessage> adapter : messageAdapters) {
				adapter.handle(message);
			}
        }
        return message;
    }
    
    public <T extends Object> T getObject(Class<T> t, Long id) {
        FindMessage<T> message = new FindMessage<T>(t, id);
        notifyInternal(message);
        return message.getResult();
    }

}
